home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / COUPON.TST / CUPONTST.C < prev    next >
C/C++ Source or Header  |  1996-02-29  |  4KB  |  139 lines

  1. /* ============ */
  2. /* cupontst.c    */
  3. /* ============ */
  4. #include <defcodes.h>
  5. #include <miscdefs.h>
  6. #include <cupndefs.h>
  7. #include <math.h>
  8. #include <mconf.h>
  9.  
  10. #define    NUM_PROBS    100
  11.  
  12. static    COUPON_DATA_STRU CouponData;
  13. static    INIT_DATA_STRU   InitialData;
  14.  
  15. /* ==================================================================== */
  16. /* CouponTest - Executes Coupon Collector's Test per Knuth        */
  17. /* ==================================================================== */
  18. void
  19. main(void)
  20. {
  21.     int        k;
  22.     long    TotVariates = 0;
  23.     double  DegFree;
  24.     double  KnMinusProb, KnMinusStat, KnPlusProb, KnPlusStat;
  25.  
  26.     double  ChiSqProb[NUM_PROBS];
  27.  
  28.     AbortGracefully();            /* Make ^C act reasonably */
  29.  
  30.     printf("\tC O U P O N  T E S T\n\n");
  31.     GetInitialData(&InitialData);
  32.     fprintf(stderr, "\n");fflush(NULL);
  33.  
  34.     /* -------------------------- */
  35.     /* Print Initial Data Entries */
  36.     /* -------------------------- */
  37.     printf("Starting Seed = %u%s\n", InitialData.UserSeed,
  38.     (InitialData.SeedSrce == (UINT)(-1)) ?
  39.         " (Unsigned Integer Part of Time of Day)" : "");
  40.  
  41.     printf("Generator     = %s\n", InitialData.GenName);
  42.  
  43.     CouponData.RandFun = InitialData.RandFun;
  44.     SetCouponControls(&CouponData);
  45.  
  46.     if (!CouponData.CallStatusOK)
  47.     {
  48.     printf("At Least One Category Has a Cell Expectation Less"
  49.         " Than Number Requested.\n");
  50.     printf("Run plancupn.exe to Determine Appropriate Number of"
  51.         " Coupons to be Counted.\n");
  52.     printf("For the Inputs That You Have Provided At Least %ld"
  53.         " Coupons are Required.\n", CouponData.UserNumCoupons);
  54.     if (CouponData.UserCellExpect != MIN_CELL_XPCT)
  55.     {
  56.         printf("For a Minimum of %d Samples Per Category At"
  57.         " Least %ld Coupons are Required.\n", MIN_CELL_XPCT,
  58.         CouponData.IdealNumCoupons);
  59.     }
  60.     }
  61.  
  62.     DegFree = CouponData.NumCategories - CouponData.NotEnough - 1;
  63.  
  64.     /* ------------------------- */
  65.     /* Generate Random Numbers,  */
  66.     /* Calculate Chi-Square Data */
  67.     /* ------------------------- */
  68.     fflush(NULL);
  69.     for (k = 0; k < NUM_PROBS; ++k)
  70.     {
  71.     CalcCouponChiSq(&CouponData);
  72.  
  73.     fprintf(stderr, "\rPass %3d (of %d), %8ld  Total Random Numbers",
  74.         k+1, NUM_PROBS, TotVariates += CouponData.TotNumGen);
  75.  
  76.     if (!CouponData.CallStatusOK)
  77.     {
  78.         printf("Generator Failed to Produce a Coupon "
  79.            "Within %ld Cycles\n", CouponData.MaxGenPerSeg);
  80.         break;
  81.     }
  82.     ChiSqProb[k] = chdtr(DegFree, CouponData.CouponChiSq);
  83.  
  84.     if (ChiSqProb[k] < 0)
  85.     {
  86.         fprintf(stderr, "\nChiSqFreq(): Function chdtr() "
  87.         "Returned Negative Probability -  Can't Happen.\n");
  88.     }
  89.  
  90.     P(printf("Coupon Chi-Square Statistic = %f\n",
  91.         CouponData.CouponChiSq));
  92.     P(printf("Total Number Variates Generated   = %ld\n",
  93.         CouponData.TotNumGen));
  94.     P(printf("Maximum No. Variates for a Coupon = %ld\n",
  95.         CouponData.ActMaxPerSeg));
  96.     P(printf("Chi-Square Probability on %.f"
  97.         " Degrees of Freedom = %.4f\n", DegFree, ChiSqProb[k]));
  98.     }
  99.  
  100.     P(printf("\nCouponData.CallStatusOK = %s\n",
  101.     (CouponData.CallStatusOK == 0) ? "FALSE" : "TRUE"));
  102.  
  103.     /* -------------------------------------------------------- */
  104.     /* Calculate K-S on Chi-Square Statistics and Probabilities */
  105.     /* -------------------------------------------------------- */
  106.     if (CouponData.CallStatusOK)
  107.     {
  108.     KSCalc(ChiSqProb, NUM_PROBS,
  109.         &KnPlusStat, &KnPlusProb,
  110.          &KnMinusStat, &KnMinusProb);
  111.  
  112.     fprintf(stderr, "\n");fflush(NULL);
  113.     printf("\nKolmogorov-Smirnov Statistics and Probabilities"
  114.         " on Chi-Square Data\n");
  115.  
  116.     if (KnPlusProb < 0)
  117.     {
  118.         printf("ERROR:  KnPlusStat = %.11e\n", KnPlusStat);
  119.     }
  120.     else
  121.     {
  122.         printf("\tK(%d)+ = %f (Knuth) or %9f%%\n", NUM_PROBS,
  123.         KnPlusStat * sqrt((double)NUM_PROBS), 100*KnPlusProb);
  124.     }
  125.  
  126.     if (KnMinusProb < 0)
  127.     {
  128.         printf("ERROR:  KnMinusStat = %.11e\n", KnMinusStat);
  129.     }
  130.     else
  131.     {
  132.         printf("\tK(%d)+ = %f (Knuth) or %9f%%\n", NUM_PROBS,
  133.         KnMinusStat * sqrt((double)NUM_PROBS), 100*KnMinusProb);
  134.     }
  135.  
  136.     printf("\nThis Run Required %ld Random Numbers\n", TotVariates);
  137.     }
  138. }
  139.